package com.hb.game.pk.ui.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.BankInfo;
import com.hb.game.pk.beans.NullBean;
import com.hb.game.pk.event.RefreshUserInfoEvent;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BindCardActivity extends BaseActivity<NullBean> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_card_num)
    EditText etCardNum;
    @BindView(R.id.et_card_addr)
    EditText etCardAddr;
    @BindView(R.id.btn_bind)
    Button btnBind;
    private String cardNum;
    private String cardAddr;
    private String name;
    MaterialDialog alertDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_card;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BindCardActivity.this.finish();
            }
        });
        BankInfo.DataBean bankInfo = APP.bankInfo;
        if (bankInfo != null) {
            if (!StringUtils.isEmpty(bankInfo.getBank_num())) {
                btnBind.setEnabled(false);
                btnBind.setText("已绑定");
                btnBind.setBackgroundResource(R.drawable.bg_btn_small_cansle_radius);
                etName.setText(bankInfo.getUser_name());
                etName.setKeyListener(null);
                etCardNum.setText(bankInfo.getBank_num());
                etCardNum.setKeyListener(null);
                etCardAddr.setText(bankInfo.getBank_info());
                etCardAddr.setKeyListener(null);
            }
        }
    }

    @OnClick(R.id.btn_bind)
    public void onViewClicked() {
        name = etName.getText().toString();
        cardAddr = etCardAddr.getText().toString();
        cardNum = etCardNum.getText().toString();

        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(cardAddr) || StringUtils.isEmpty(cardNum)) {
            ToastUtils.showToast("请正确填写信息");
            return;
        }
        if (alertDialog == null) {
            View inflate = View.inflate(this, R.layout.dialog_bind_card_alert, null);
            TextView tvCansle = (TextView) inflate.findViewById(R.id.tv_cansle);
            TextView tvOk = (TextView) inflate.findViewById(R.id.tv_ok);
            tvCansle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (alertDialog.isShowing()) {
                        alertDialog.dismiss();
                    }
                }
            });
            tvOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    requestNet();
                    if (alertDialog.isShowing()) {
                        alertDialog.dismiss();
                    }
                }
            });
            setDialogData(inflate);
            alertDialog = new MaterialDialog.Builder(this)
                    .customView(inflate, false)
                    .show();
        } else {
            View customView = alertDialog.getCustomView();
            setDialogData(customView);
            if (alertDialog.isShowing()) {
                alertDialog.dismiss();
            } else {
                alertDialog.show();
            }
        }

    }

    private void setDialogData(View inflate) {
        TextView tvName = (TextView) inflate.findViewById(R.id.tv_name);
        TextView tvCardNum = (TextView) inflate.findViewById(R.id.tv_card_num);
        TextView tvCardAddr = (TextView) inflate.findViewById(R.id.tv_card_addr);
        tvName.setText(name);
        tvCardNum.setText(cardNum);
        tvCardAddr.setText(cardAddr);
    }

    private void requestNet() {
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).add_bank(name, cardNum, cardAddr);
        call.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                ToastUtils.showToast(body.getInfo());
                EventBus.getDefault().post(new RefreshUserInfoEvent());
                BindCardActivity.this.finish();
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }
}
